import 'package:flutter/material.dart';

import 'package:get/get.dart';
import 'package:im_demo/app/data/chat/message.dart';

import '../../../../utils/logger_util.dart';
import '../../../data/chat/action_type.dart';
import '../controllers/chat_controller.dart';

class ChatView extends GetView<ChatController> {
  const ChatView({super.key});
  @override
  Widget build(BuildContext context) {
    final controller = Get.put(ChatController());

    return Scaffold(
      appBar: AppBar(title: const Text('Chat'), centerTitle: true),
      body: Center(
        child: Container(
          padding: EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 8.0),
          child: Column(
            mainAxisSize: MainAxisSize.max,
            children: [
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Flexible(
                    flex: 7,
                    child: TextField(
                      keyboardType: TextInputType.text,
                      controller: controller.urlEditTextController,
                      decoration: InputDecoration(
                        hintText: '请输入地址',
                        // counter: Text(''),
                        counterText: '',
                        border: OutlineInputBorder(),
                        contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
                        // contentPadding: EdgeInsets.only(bottom: 0.0),
                        suffixIcon: IconButton(
                          onPressed: () {
                            logE('clean button click');

                            controller.cleanInputText();
                          },
                          iconSize: 16.0,
                          icon: Icon(Icons.close),
                        ),
                      ),
                      onChanged: (text) {
                        // controller.updateInputText(text);

                        // logE('onChanged text:$text');
                      },
                    ),
                  ),
                  SizedBox(width: 8.0),
                ],
              ),
              SizedBox(height: 8.0),
              Row(
                children: [
                  ElevatedButton(
                    onPressed: () {
                      final text = controller.editTextController.text;
                      logE('send text:$text');
                      controller.connectWebsocket();
                    },
                    style: ElevatedButton.styleFrom(
                      minimumSize: const Size(64.0, 48.0),
                    ),
                    child: Text('连接', style: TextStyle(fontSize: 14.0)),
                  ),
                  SizedBox(width: 8.0),
                  ElevatedButton(
                    onPressed: () {
                      final text = controller.editTextController.text;
                      logE('send text:$text');
                      controller.disconnectWebsocket();
                    },
                    style: ElevatedButton.styleFrom(
                      minimumSize: const Size(64.0, 48.0),
                    ),
                    child: Text('断开', style: TextStyle(fontSize: 14.0)),
                  ),
                ],
              ),
              SizedBox(height: 16.0),
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Flexible(
                    flex: 10,
                    child: TextField(
                      keyboardType: TextInputType.text,
                      controller: controller.editTextController,
                      decoration: InputDecoration(
                        hintText: '请输入内容',
                        // counter: Text(''),
                        counterText: '',
                        border: OutlineInputBorder(),
                        contentPadding: EdgeInsets.fromLTRB(12, 20, 12, 0),
                        // contentPadding: EdgeInsets.only(bottom: 0.0),
                        suffixIcon: IconButton(
                          onPressed: () {
                            logE('clean button click');

                            controller.cleanInputText();
                          },
                          iconSize: 16.0,
                          icon: Icon(Icons.close),
                        ),
                      ),
                      onChanged: (text) {
                        // controller.updateInputText(text);

                        // logE('onChanged text:$text');
                      },
                    ),
                  ),
                  SizedBox(width: 8.0),
                  Flexible(
                    flex: 3,
                    child: SizedBox(
                      // height: 56.0,
                      child: ElevatedButton(
                        onPressed: () {
                          final text = controller.editTextController.text;
                          logE('send text:$text');
                          controller.sendMessage(text);
                        },
                        style: ElevatedButton.styleFrom(
                          minimumSize: const Size(64.0, 48.0),
                        ),
                        child: Text('发送', style: TextStyle(fontSize: 14.0)),
                      ),
                    ),
                  ),
                ],
              ),
              SizedBox(height: 8.0),
              Expanded(
                child: Obx(
                  () => ListView.builder(
                    itemBuilder:
                        (_, index) =>
                            _messageItem(controller.messageList[index]),
                    reverse: false,
                    itemCount: controller.messageList.length,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _messageItem(Message message) {
    String action = '';
    switch (message.actionType) {
      case ActionType.send:
        action = "发送：";
        break;
      case ActionType.receive:
        action = "收到：";
        break;
      default:
        action = "";
        break;
    }
    final msg = action + message.content;

    return Container(
      padding: EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 8.0),
      child: Text(msg),
    );
  }
}
